{% extends "stocksys/base/base_page.html" %}
{% load staticfiles %}
{% block headscript %}
    {% csrf_token %}
{% endblock %}
{% block content %}
    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <form id="MessageSend" class="form-horizontal" action="" method="post"
                              novalidate="novalidate">
                            <div class="content">
                                <h4 class="title">站内信&邮件发送</h4>
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">
                                            发送到用户名
                                        </label>
                                        <div class="col-sm-4">
                                            <input class="form-control"
                                                   type="text"
                                                   id="username"
                                                   name="username"
                                                   minLength="5"
                                            />
                                        </div>
                                        <div class="col-sm-4"><p>*</p></div>
                                        <div class="col-sm-2">
                                            <label class="checkbox">
                                                <input type="checkbox" id="use_email" name="use_email"
                                                       data-toggle="checkbox"
                                                       value="">启用邮件通知
                                            </label>
                                        </div>
                                    </div>
                                </fieldset>
                                <fieldset id="email-form-title">
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">
                                            标题
                                        </label>
                                        <div class="col-sm-6">
                                            <input class="form-control"
                                                   type="text"
                                                   id="title"
                                                   name="title"
                                                   minLength="1"
                                            />
                                        </div>
                                        <div class="col-sm-4"><p>*</p></div>
                                    </div>
                                </fieldset>
                                <fieldset id="email-form-content">
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"></label>
                                        <div class="col-sm-10">
                                            <textarea class="form-control" name="content" placeholder="邮件内容"
                                                      rows="10"></textarea>
                                        </div>
                                    </div>
                                </fieldset>
                                <div class="card-footer text-center">
                                    <button type="submit" class="btn btn-info btn-fill">发送</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block pagescript %}
    <script>

        $().ready(function () {
            var token = $.cookie('csrftoken');

            $('#MessageSend').validate({
                debug: false,
                ignore: ":hidden",
                rules: {
                    email: {
                        required: function () {
                            if ($("#use_email").prop("checked"))
                                return true;
                            else return false
                        },
                        minlength: 3,
                    },
                    title: {
                        required: true,
                        minlength: 3,

                    },
                    content: {
                        required: true,
                        minlength: 10,
                    },
                    username: {
                        required: true,
                        minlength: 3,
                        remote: {
                            url: "/stocksys/user_validate/",     //后台处理程序
                            type: "post",               //数据发送方式
                            headers: {'X-CSRFToken': token},
                            dataType: "json",           //接受数据格式
                            data: {                     //要传递的数据
                                username: function () {
                                    return $("#username").val();
                                },
                            }
                        },
                    },
                },
                messages: {
                        email: "请输入正确的邮箱地址",
                        title: {
                            required: "必须输入标题",
                            minlength: "标题长度至少为3"
                        },
                        content: {
                            required: "需要发送的消息不能为空",
                            minlength: "需要发送的消息长度至少为10"
                        },
                        username:
                            {
                                required: "用户不能为空",
                                minlength:
                                    "用户名至少大于5位",
                                remote:
                                    "用户不存在",
                            }
                        ,
                    }
                ,
                submitHandler: function (form) {
                    var token = $.cookie('csrftoken');
                    if ($("#use_email").prop("checked")) {
                        $("#use_email").val("True")
                    }
                    $(form).ajaxSubmit({
                        url: "{% url 'stocksys:send_message' %}",
                        headers: {'X-CSRFToken': token},
                        dataType: "json",
                        beforeSubmit: function () {
                            console.log(this);
                        },
                        success: function (data) {
                            swal({
                                title: "发送成功",
                                text: "成功发送消息",
                                buttonsStyling: false,
                                confirmButtonClass: "btn btn-success",
                                type: "success"
                            }).catch(swal.noop);
                        }
                    });
                }
                ,
            });
        })
        ;
    </script>
{% endblock %}

